<?php
# ============================================================================
# 版权所有  Chilcoo.com，并保留所有权利。
# 网站地址: http://www.chilcoo.com/erphp
# ----------------------------------------------------------------------------
# 这不是一个自由软件！你不能通过任何方式破解本软件.
# 使用:不允许对程序代码以任何形式任何目的的再发布。
# ============================================================================
# $Author: Chilcoo Inc. *
# $Official website : www.chilcoo.com
# 
# 文件信息:
# File : /cate/index.php
# 分类列表页面

# 载入常用集
# 导入common
require_once (dirname(__FILE__).'/../../include/common.inc.php');
require_once (dirname(__FILE__).'/../../include/_manager/login.inc.php');
# 页面级别
$conf_web_page_level = 4;

class page_index extends page{
	public $listview;
	function __construct(){
		$this->database = new db(config::conf_db_host,config::conf_db_user,
			config::conf_db_password,config::conf_db_name);
		$this->listview = array();
	}
	function category_len(){
		$len = 0;
		try{
			$sql = "SELECT cs2_cate.name FROM cs2_cate;";
			$len = $this->database->count($sql);
		}catch (Exception $e) {
			$len = -1;
		}
		return $len;
	}
	function category_list($rootId){
		$sql = "SELECT b.cateId , b.name , b.rank, b.rootId , b.note 
			FROM cs2_cate AS a, cs2_cate AS b 
			WHERE (a.cateId=b.rootId) AND (a.cateId=".$rootId." or b.rootId=".$rootId.")
			ORDER BY b.rank ASC";
		//array_push(list, data);
		$this->listview[$rootId]= $this->database->select($sql);
		if(count($this->listview[$rootId]) == 0){
			unset($this->listview[$rootId]);
			return;
		}
			for ($i = 0; $i < count($this->listview[$rootId]); $i++) {
				$this->category_list($this->listview[$rootId][$i]['cateId']);
			}
		
	}
	
	function delete_category_by_rootId($rootId){
		$sql = "SELECT cs2_cate.cateId FROM cs2_cate WHERE (cs2_cate.rootId = ".$rootId.");";
		$rows = $this->database->select($sql);
		// 
		foreach($rows as &$value){
			$sql = "DELETE FROM cs2_cate WHERE (cs2_cate.cateId=".$value['cateId'].")";
			$this->database->delete($sql);
			$this->delete_category_by_rootId($value['cateId']);
		}
	}
	
	function ajax_callback(){
		# POST 数据
		$token = $_POST['token'];
		# 初始返回用 json
		$json = array("ref"=>"error");
		if($token == config::TOKEN_UPDATE_RANK){
			$rank = $_POST['rank'];
			$cateId = $_POST['cateId'];
			$sql = "UPDATE cs2_cate SET rank='".$rank."' WHERE (cateId =". $cateId.");";
			try {
				$this->database->update($sql);
				$json = array("ref"=>"OK");
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		if($token == config::TOKEN_ADD_CATE){
			global $config;
			$name = $_POST['name'];
			$rank = $_POST['rank'];
			$cateId = $_POST['cateId'];
			
			$sql = "INSERT INTO cs2_cate (name, note,rank,rootId,token) 
				VALUES ('".$name."','',".$rank.",".$cateId.",'".$config->guid()."');";
			try {
				$this->database->insert($sql);
				$json = array("ref"=>"OK");
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		if($token == config::TOKEN_UP_CATE)
		{
			$name = $_POST['name'];
			$cateId = $_POST['cateId'];
			$sql = "UPDATE cs2_cate SET name='".$name."' WHERE (cateId =". $cateId.");";
			try {
				$this->database->update($sql);
				$json = array("ref"=>"OK","name"=>$name,"cateId"=>$cateId);
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		if($token == config::TOKEN_DEL_CATE){
			$cateId = $_POST['cateId'];
			$this->delete_category_by_rootId($cateId);
			$sql = "DELETE FROM cs2_cate WHERE (cs2_cate.cateId=".$cateId.")";
			try {
				$this->database->delete($sql);
				
				$json = array("ref"=>"OK","name");
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		return json_encode($json);
	}
}

$pg = new page_index();
$pg->category_list(1);
# Session 和 POST 安全访问.
if($_SERVER['REQUEST_METHOD'] != 'POST'){
	$_SESSION[config::conf_session_id_token] = $config->web_page_period;
	$config->smarty->assign('category_len', $pg->category_len());
	$config->smarty->assign('category_list', $pg->listview);
	# 加载页面
	$pg->render($config->temp_page_path.config::conf_web_template_extension);
}else{
	print_r($pg->ajax_callback());
}


?>